package top.ljlgithub.api.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.ljlgithub.api.bean.PageBean;
import top.ljlgithub.api.bean.PageCondition;
import top.ljlgithub.api.bean.User;
import top.ljlgithub.api.common.response.Meta;
import top.ljlgithub.api.common.response.ResponseEntity;
import top.ljlgithub.api.exception.ParamException;
import top.ljlgithub.api.service.UserService;

import java.util.List;

/**
 * @ClassName
 * @Description TODO
 * @Author L1470
 * @Date 2021/5/5 15:46
 * @Version 1.0
 **/
@RestController
@CrossOrigin
public class UserController {
    @Autowired
    UserService userService;

    @RequiresPermissions("user:getAllManagers:*")
    @GetMapping(value = "/users")
    public ResponseEntity<Object> findUserByPage(PageCondition pageCondition) throws ParamException {
        String query = pageCondition.getQuery();
        Integer pagesize = pageCondition.getPageSize();
        Integer pagenum = pageCondition.getPageNum();
        if (pagesize == null || pagenum == null){
            throw new ParamException();
        }
        Meta meta = new Meta("获取成功", 200);
        Integer total = userService.getTotal(query);
        List<User> users = userService.userList(query, pagesize, pagenum);
        PageBean<User> data = new PageBean<>(total,pagenum,users);
        ResponseEntity<Object> result = new ResponseEntity<>(data,meta);
        return result;
    }
}
